package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.service.AccountStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/accountStatistics")
public class AccountStatisticsController {

    @Autowired
    AccountStatisticsService accountStatisticsService;

    //@RequiresPermissions("RegisterStatistics:view")
    @RequestMapping(value = "/registerLine", method = RequestMethod.GET)
    public JSONObject RegisterLine(@RequestParam(value="type",defaultValue = "app_channel") String type,
                                   @RequestParam(value="channel", required=false) String channel,
                                   @RequestParam(value="version", required=false) String version,
                                   @RequestParam(value="system", required=false) String system,
                                   @RequestParam(value="gameType", required=false) String gameType,
                                   @RequestParam(value="appPackage", required=false) String appName,
                                   @RequestParam(value="startTime", required=false) String startTime,
                                   @RequestParam(value="endTime", required=false) String endTime,
                                   @RequestParam(value="redirect", defaultValue="false") boolean redirect,
                                   @RequestParam(value="gender", required=false) Integer gender)
    {   JSONObject res;
        res = accountStatisticsService.getRegisterLine(type,channel, version, system, gameType, appName, startTime, endTime, redirect, gender);
        return new BaseResponse<>(0,"success", res);
    }

    //@RequiresPermissions("RegisterStatistics:view")
    @RequestMapping(value = "/registerList", method = RequestMethod.GET)
    public JSONObject RegisterList(@RequestParam(value="channel", required=false) String channel,
                                   @RequestParam(value="version", required=false) String version,
                                   @RequestParam(value="system", required=false) String system,
                                   @RequestParam(value="gameType", required=false) String gameType,
                                   @RequestParam(value="appPackage", required=false) String appName,
                                   @RequestParam(value="startTime", required=false) String startTime,
                                   @RequestParam(value="endTime", required=false) String endTime,
                                   @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                   @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                   @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                   @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy,
                                   @RequestParam(value="redirect", defaultValue="false") boolean redirect
                                   )
    {
        if (channel==null && appName==null && system==null && version==null) {
            return accountStatisticsService.getUserDailyStatisticSumList("register", startTime, endTime, offset, limit);
        }

        JSONArray dataItems = new JSONArray();
        int count = accountStatisticsService.getRegisterCount(channel, version, system, gameType, appName, startTime, endTime);
        accountStatisticsService.getRegisterList(channel, version, system, gameType, appName, startTime, endTime, offset, limit, orderType, orderBy, redirect).forEach(
                x->{
                    dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
                });

        BaseResponse results = new BaseResponse(dataItems, count);
        return results;
    }

    //@RequiresPermissions("RegisterStatistics:view")
    @RequestMapping(value = "/loginLine", method = RequestMethod.GET)
    public JSONObject LoginLine(@RequestParam(value="type", defaultValue = "app_channel") String type,
                                @RequestParam(value="channel", required=false) String channel,
                                @RequestParam(value="version", required=false) String version,
                                @RequestParam(value="system", required=false) String system,
                                @RequestParam(value="gameType", required=false) String gameType,
                                @RequestParam(value="appPackage", required=false) String appName,
                                @RequestParam(value="startTime", required=false) String startTime,
                                @RequestParam(value="endTime", required=false) String endTime,
                                @RequestParam(value="redirect", defaultValue="false") boolean redirect,
                                @RequestParam(value="gender", required=false) Integer gender)
    {
        JSONObject res = accountStatisticsService.getLoginLine(type, channel, version, system, gameType, appName, startTime, endTime, redirect, gender);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/loginList", method = RequestMethod.GET)
    public JSONObject loginList(@RequestParam(value="channel", required=false) String channel,
                                @RequestParam(value="version", required=false) String version,
                                @RequestParam(value="system", required=false) String system,
                                @RequestParam(value="gameType", required=false) String gameType,
                                @RequestParam(value="appPackage", required=false) String appName,
                                @RequestParam(value="startTime", required=false) String startTime,
                                @RequestParam(value="endTime", required=false) String endTime,
                                @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                @RequestParam(value="orderBy", required=false, defaultValue="target_date") String orderBy,
                                @RequestParam(value="redirect", defaultValue="false") boolean redirect
    )
    {
        if (channel==null && appName==null && system==null && version==null) {
            return accountStatisticsService.getUserDailyStatisticSumList("active", startTime, endTime, offset, limit);
        }

        JSONArray dataItems = new JSONArray();
        int count = accountStatisticsService.getLoginCount(channel, version, system, gameType, appName, startTime, endTime);
        accountStatisticsService.getLoginList(channel, version, system, gameType, appName, startTime, endTime, offset, limit, orderType, orderBy, redirect).forEach(
            x->{
                dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
            });

        BaseResponse results = new BaseResponse(dataItems, count);
        return results;
    }


    //@RequiresPermissions("RegisterStatistics:view")
    @RequestMapping(value = "/remainLine", method = RequestMethod.GET)
    public JSONObject remainLine(@RequestParam(value="appPackage", required=false) String appName,
                                 @RequestParam(value="channel", required=false) String channel,
                                 @RequestParam(value="version", required=false) String appVersion,
                                 @RequestParam(value="system", required=false) String system,
                                 @RequestParam(value="startTime", required=false) String startTime,
                                 @RequestParam(value="endTime", required=false) String endTime,
                                 @RequestParam(value="redirect", defaultValue="false") boolean redirect,
                                 @RequestParam(value="gender", required=false) Integer gender
                                 )
    {
        JSONObject res = accountStatisticsService.getRemainLine(appName, channel,appVersion, system, startTime, endTime, redirect, gender);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/remainList", method = RequestMethod.GET)
    public JSONObject remainList(@RequestParam(value="appPackage", required=false) String appName,
                                 @RequestParam(value="channel", required=false) String channel,
                                 @RequestParam(value="version", required=false) String appVersion,
                                 @RequestParam(value="system", required=false) String system,
                                 @RequestParam(value="startTime", required=false) String startTime,
                                 @RequestParam(value="endTime", required=false) String endTime,
                                 @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                 @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                 @RequestParam(value="redirect", defaultValue="false") boolean redirect,
                                 @RequestParam(value="gender", required=false) Integer gender
    )
    {
        if (channel==null && appName==null && system==null && appVersion==null && gender==null) {
            return accountStatisticsService.getUserDailyStatisticSumList("registerRemain", startTime, endTime, offset, limit);
        }

        return accountStatisticsService.getRemainList(appName,channel, appVersion, system, startTime, endTime, offset, limit, redirect, gender);
    }

    @RequestMapping(value = "/activeRemainLine", method = RequestMethod.GET)
    public JSONObject activeRemainLine(@RequestParam(value="appPackage", required=false) String appName,
                                       @RequestParam(value="channel", required=false) String channel,
                                       @RequestParam(value="version", required=false) String appVersion,
                                       @RequestParam(value="system", required=false) String system,
                                       @RequestParam(value="startTime", required=false) String startTime,
                                       @RequestParam(value="endTime", required=false) String endTime,
                                       @RequestParam(value="redirect", defaultValue="false") boolean redirect,
                                       @RequestParam(value="gender", required=false) Integer gender)
    {
        JSONObject res = accountStatisticsService.getActiveRemainLine(appName, channel,appVersion, system, startTime, endTime, redirect, gender);
        return new BaseResponse<>(0,"success", res);
    }

    @RequestMapping(value = "/activeRemainList", method = RequestMethod.GET)
    public JSONObject activeRemainList(@RequestParam(value="appPackage", required=false) String appName,
                                       @RequestParam(value="channel", required=false) String channel,
                                       @RequestParam(value="version", required=false) String appVersion,
                                       @RequestParam(value="system", required=false) String system,
                                       @RequestParam(value="startTime", required=false) String startTime,
                                       @RequestParam(value="endTime", required=false) String endTime,
                                       @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                       @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                       @RequestParam(value="redirect", defaultValue="false") boolean redirect,
                                       @RequestParam(value="gender", required=false) Integer gender
    )
    {
        if (channel==null && appName==null && system==null && appVersion==null && gender==null) {
            return accountStatisticsService.getUserDailyStatisticSumList("activeRemain", startTime, endTime, offset, limit);
        }

        return accountStatisticsService.getActiveRemainList(appName, channel, appVersion, system, startTime, endTime, offset, limit, redirect, gender);
    }

    @RequestMapping(value = "/register_data", method = RequestMethod.GET)
    public BaseRsp NewUserData(@RequestParam(value="app_version",required = false) String appVersion,
                               @RequestParam(value="start_time", required=false) String startTime,
                               @RequestParam(value="end_time", required=false) String endTime,
                               @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                               @RequestParam(value="limit", required=false, defaultValue="20") int limit)
    {
        return accountStatisticsService.getNewUserData(appVersion, startTime, endTime, offset, limit);
    }

    @RequestMapping(value = "/active_data", method = RequestMethod.GET)
    public BaseRsp ActiveUserData(@RequestParam(value="start_time", required=false) String startTime,
                                  @RequestParam(value="end_time", required=false) String endTime,
                                  @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                  @RequestParam(value="limit", required=false, defaultValue="20") int limit)
    {
        return accountStatisticsService.getActiveUserData(startTime, endTime, offset, limit);
    }
}
